/**
 * 添加一个Ext.Msg.ghost提示方法
 */
Ext.MyApp = function(){
    var msgCt;

    function createBox(t, s){
        return ['<div class="msg">',
            '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
            '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s,
            '</div></div></div>', '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
            '</div>'].join('');
    }
    return {
        ghost : function(title, format){
            if(!msgCt){
                msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
            }
            msgCt.center();
            var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
            var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
            m.slideIn('t').highlight().pause(2).ghost("t", {remove:true});
        }
    };
}();
Ext.Msg.ghost=Ext.MyApp.ghost;

Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);
function checkUserSessionStatus(conn,response,options){
    //Ext重新封装了response对象
    if(response.getResponseHeader !=null && response.getResponseHeader('sessionstatus')){
        alert("出于安全原因，请重新登录！");
        //发现请求超时，退出处理代码...
        var win = window;
        while (win != win.parent){
            win = win.parent;
        }
        win.location = "index.jsp";
    }
}

Ext.apply(Ext.form.VTypes, {
    daterange : function(val, field) {
        var date = field.parseDate(val);

        if(!date){
            return false;
        }
        if (field.startDateField) {
            var start = Ext.getCmp(field.startDateField);
            if (!start.maxValue || (date.getTime() != start.maxValue.getTime())) {
                start.setMaxValue(date);
                start.validate();
            }
        }
        else if (field.endDateField) {
            var end = Ext.getCmp(field.endDateField);
            if (!end.minValue || (date.getTime() != end.minValue.getTime())) {
                end.setMinValue(date);
                end.validate();
            }
        }
        return true;
    }
});

//扩展TreeLoader，以适合strtus和spring mvc
Ext.tree.JsonPluginTreeLoader = function(config){
    this.rootName='root';
    Ext.tree.JsonPluginTreeLoader.superclass.constructor.call(this,config);
}
Ext.extend(Ext.tree.JsonPluginTreeLoader,Ext.tree.TreeLoader,{
    processResponse: function(response, node, callback){
        var json = response.responseText;
        try{
            var json = eval("(" + json + ")");
            node.beginUpdate();
            var o = json["key"];
            for(var i=0,len=o.length; i<len; i++){
                var n = this.createNode(o[i]);
                if(n){
                    node.appendChild(n);
                }
            }
            node.endUpdate();
            if(typeof callback == "function"){
                callback(this,node);
            }
        } catch(e) {
            this.handleFailure(response);
        }
    }
});

//解决Ext3的formPanel通过grid的store查询问题
function dateFormat(value){
    if(!(null == value || value=='')){
        return Ext.util.Format.date(new Date(value),'Y-m-d');
    }else{
        return '';
    }
}
function dateTimeFormat(value){
    if(!(null == value || value=='')){
        return Ext.util.Format.date(new Date(value),'Y-m-d H:i:s');
    }else{
        return '';
    }
}